package cn.yangyumanage.model;

import java.io.IOException;
import java.util.List;

import cn.yangyumanage.application.DataApplication;
import cn.yangyumanage.bean.Film;
import cn.yangyumanage.bean.ImageQuality;
import cn.yangyumanage.bean.LargeClassification;
import cn.yangyumanage.bean.SmallClassification;
import cn.yangyumanage.bean.TheRegion;
import cn.yangyumanage.bean.TheYear;
import cn.yangyumanage.bean.util.FilmContainer;
import cn.yangyumanage.bean.util.ImageQualityContainer;
import cn.yangyumanage.bean.util.LargeClassificationContainer;
import cn.yangyumanage.bean.util.NormalContainer;
import cn.yangyumanage.bean.util.RegionContainer;
import cn.yangyumanage.bean.util.SmallClassificationContainer;
import cn.yangyumanage.bean.util.YearContainer;
import cn.yangyumanage.callback.DataCallBack;
import cn.yangyumanage.callback.HttpCallBack;
import cn.yangyumanage.config.IpConfig;
import cn.yangyumanage.http.HttpClient;
import cn.yangyumanage.util.Aes;

public class FilmAddHttpModel implements BaseModel{


    //利用豆瓣ID获取影片详细信息
    public void getFilmDetails(String sid, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/films/details?sid=" + sid;
        HttpClient.asyncGet(url, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
//                    Log.d("getFilmDetails", decrypt);
                    FilmContainer container = DataApplication.gson.fromJson(decrypt, FilmContainer.class);
                    Film film = container.getData();
                    dataCallBack.getData(film);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.ADMIN);
    }

    //获取所有大分类
    public void getAllLarge(DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/classifications/large";
        HttpClient.asyncGet(url, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    LargeClassificationContainer container = DataApplication.gson.fromJson(decrypt, LargeClassificationContainer.class);
                    List<LargeClassification> data = container.getData();
                    dataCallBack.getData(data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.ADMIN);
    }

    //获取指定大分类id所关联的所有小分类
    public void getAllRelatedSmallClassification(int id, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/public/largesmall/" + id;
        HttpClient.asyncGet(url, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    SmallClassificationContainer container = DataApplication.gson.fromJson(decrypt, SmallClassificationContainer.class);
                    List<SmallClassification> data = container.getData();
                    dataCallBack.getData(data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.PUBLIC);
    }

    //获取指定大分类id所关联的所有地区
    public void getAllRelatedRegion(int id, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/public/largeregion/" + id;
        HttpClient.asyncGet(url, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    RegionContainer container =DataApplication.gson.fromJson(decrypt, RegionContainer.class);
                    List<TheRegion> data = container.getData();
                    dataCallBack.getData(data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.PUBLIC);
    }

    //获取指定大分类id所关联的所有年份
    public void getAllRelatedYear(int id, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/public/largeyear/" + id;
        HttpClient.asyncGet(url, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    YearContainer container = DataApplication.gson.fromJson(decrypt, YearContainer.class);
                    List<TheYear> data = container.getData();
                    dataCallBack.getData(data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.PUBLIC);
    }

    //获取全部画质
    public void getAllImageQuality(DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/imagequalitys";
        HttpClient.asyncGet(url, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    ImageQualityContainer container = DataApplication.gson.fromJson(decrypt, ImageQualityContainer.class);
                    List<ImageQuality> data = container.getData();
                    dataCallBack.getData(data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.ADMIN);
    }

    //判读库中是否存在指定sid的影片
    public void querySidExists(String sid, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/films?sid=" + sid;
        HttpClient.asyncGet(url, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    NormalContainer container = DataApplication.gson.fromJson(decrypt, NormalContainer.class);
                    dataCallBack.getData(container.getFlag());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.ADMIN);
    }

    //添加影片
    public void addFilm(Film film, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/films";
        String json = DataApplication.gson.toJson(film);
        HttpClient.asyncPost(url, json, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    NormalContainer container = DataApplication.gson.fromJson(decrypt, NormalContainer.class);
                    dataCallBack.getData(container.getFlag());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.ADMIN);
    }

    @Override
    public void onDestroy() {

    }
}
